package com.ming.elm.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ming.elm.dao.CartDao;
import com.ming.elm.po.Cart;
import com.ming.elm.service.CartService;
import com.ming.elm.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

public class CartServiceImpl implements CartService{

	@Override
	public int saveCart(Cart cart) {
		int result = 0;
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		CartDao mapper = sqlSession.getMapper(CartDao.class);
		try {
			result = mapper.saveCart(cart);
		} catch (Exception e) {
			e.printStackTrace();
			// 回滚事务
			sqlSession.rollback();
		}finally {
			// 提交事务
			sqlSession.commit();
			sqlSession.close();
		}

		return result;
	}
	
	@Override
	public int updateCart(Cart cart) {
		int result = 0;
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		CartDao mapper = sqlSession.getMapper(CartDao.class);
		try {
			result = mapper.updateCart(cart);
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.commit();
			sqlSession.close();
		}

		return result;
	}
	
	@Override
	public int removeCart(Cart cart) {
		int result = 0;
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		CartDao mapper = sqlSession.getMapper(CartDao.class);
		try {
			result = mapper.removeCart(cart);
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.commit();
			sqlSession.close();
		}

		return result;
	}
	
	@Override
	public List<Cart> listCart(Cart cart){
		List<Cart> list = null;

		SqlSession sqlSession = MybatisUtil.getSqlSession();
		CartDao mapper = sqlSession.getMapper(CartDao.class);
		try {
			list = mapper.listCart(cart);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}
}
